import { createSlice } from "@reduxjs/toolkit";
import { reqCartList } from '../../api/CartList/index'
const cartListStore = createSlice({
    name: 'cartList',
    initialState: {
        // 购物车列表数据
        cartList: []
    },
    reducers: {

        updateCartList(state, action) {
            state.cartList = action.payload
        }
    }
})
//获取购物车列表数据
const fetchCartList = () => {
    return async (dispatch: any) => {
        try {
            const res = await reqCartList()
            const data = res.result
            console.log('redux测试购物车:', data)
            dispatch(updateCartList(data))
        }
        catch (err) {
            console.log(err)
        }
    }
}

export const { updateCartList } = cartListStore.actions
export default cartListStore.reducer
export { fetchCartList }